﻿using DotNetNuke.Services.Exceptions;
using DotNetNuke.UI.Skins;
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace C19.SkinObject
{
    public partial class TouchIcon : SkinObjectBase
    {

        #region Public Members

        public enum TouchIconRel
        {
            AppleTouchIcon,
            AppleTouchIconPrecomposed,
            Icon,
            ShortcutIcon
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets and sets the Sizes property.
        /// </summary>
        public string Sizes { get; set; }

        /// <summary>
        /// Gets and sets the Href property.
        /// </summary>
        public string Href { get; set; }

        /// <summary>
        /// Gets and sets the Rel property.
        /// </summary>
        public TouchIconRel Rel { get; set; }

        #endregion

        #region Base Function Overrides

        /// <summary>
        /// Overrides the OnLoad function.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                base.OnLoad(e);

                // create link control
                var touchIcon = new HtmlGenericControl("link");

                // set rel property
                string relValue = GetRelValue(Rel);
                touchIcon.Attributes.Add("rel", relValue);

                // set sizes property
                if (!string.IsNullOrEmpty(Sizes))
                {
                    touchIcon.Attributes.Add("sizes", Sizes);
                }

                // set href property
                if (!string.IsNullOrEmpty(Href))
                {
                    touchIcon.Attributes.Add("href", Href);
                }

                // add touchIcon link to the page head
                Page.Header.Controls.Add(touchIcon);
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        #endregion

        #region Private Functions

        /// <summary>
        /// Gets the string representation of the TouchIconRel value.
        /// </summary>
        /// <param name="rel">TouchIcon rel value.</param>
        /// <returns>String value of TouchIconRel.</returns>
        private string GetRelValue(TouchIconRel rel)
        {
            var result = "";
            try
            {
                switch (rel)
                {
                    case TouchIconRel.AppleTouchIcon:
                        result = "apple-touch-icon";
                        break;
                    case TouchIconRel.AppleTouchIconPrecomposed:
                        result = "apple-touch-icon-precomposed";
                        break;
                    case TouchIconRel.Icon:
                        result = "icon";
                        break;
                    case TouchIconRel.ShortcutIcon:
                        result = "shortcut icon";
                        break;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        #endregion
    }
}